package com.lisp.algorithm.select;

public class MaxAndMin {

    public static void main(String[] args) {
        int[] a = {1, 8, 2, 4};
        maxAndMin(a);
        
        a = new int[] {1, 8 ,2};
        maxAndMin(a);
    }
    
    public static void maxAndMin(int a[]) {
        int max = a[0];
        int min = a[0];
        int i=0;
        for(; i<a.length-1; i=i+2) {
            if(a[i] <= a[i+1]) {
                if(a[i] < min) {
                    min = a[i];
                } 
                
                if(a[i+1] > max) {
                    max = a[i+1];
                }
            } else {
                if(a[i] > max) {
                    max = a[i];
                }
                
                if(a[i+1] < min) {
                    min = a[i+1];
                }
            }
        }
        
        if(i == a.length - 1) {
            if(a[i] > max) {
                max = a[i];
            }
            
            if(a[i] < min) {
                min = a[i];
            }
        }
        
        System.out.println(max + ", " + min);
    }
}
